<template>
  <div>
    <el-row style="margin: 10px;">
      <el-col :span="3">
        <el-button type="primary" size="medium" @click="dialogTableVisible = true">新增销毁单
        </el-button>
      </el-col>
      <el-col :span="3">
        <el-button type="primary" size="medium" @click="addDestroy()">销毁</el-button>
      </el-col>
      <el-col :span="3">
        <el-button type="danger" size="medium" @click="clearDrugCheck()">清空销毁单</el-button>
      </el-col>
    </el-row>
    <el-row style="margin: 10px;">
      <el-col>
        销毁单: <el-input style="width: 170px;" v-model="ruleFrom.destroyId" :readonly="true"></el-input>
      </el-col>
    </el-row>
    <el-row style="margin: 10px;">
      销毁备注:<el-input type="textarea" :rows="2" style="width: 500px;"  placeholder="请输入内容" v-model="ruleFrom.destroyNote" ></el-input>
    </el-row>


    <el-table :data="ruleFrom.lyhCheckDetailsEntities.slice((currentPage-1)*pagesize,currentPage*pagesize)"
              style="width: 100%"
              @selection-change="selectionLineChangeHandle2"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="盘点号" prop="checkId"/>
      <el-table-column label="药品名" prop="lyhDrugEntity.drugName"></el-table-column>
      <el-table-column label="实际数量" prop="checkNum"></el-table-column>
      <el-table-column label="亏损数量" prop="checkKnum2"></el-table-column>
      <el-table-column prop="piCi" label="批次号"/>
      <el-table-column label="备注" prop="checkNote"></el-table-column>
      <el-table-column label="盘点日期" prop="detailsDate"/>
    </el-table>
    <div class="fy_div" style="margin-left: 400px">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[5, 10, 20, 40]"
          :page-size="pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="tableDate.length">
      </el-pagination>
    </div>
    <el-dialog title="选择销毁单" v-model="dialogTableVisible" width="20%">
      <el-select v-model="ruleFrom.checkId">
        <el-option v-for="item in options" :key="item.checkId" :label="item.checkId" :value="item.checkId">
        </el-option>
      </el-select>
      <el-row style="margin: 10px;">
        <el-col :span="6">
          <el-button type="primary" size="medium" @click="dialogTableVisible=false,initDate()">
            确定
          </el-button>
        </el-col>
        <el-col :span="6" :push="4">
          <el-button type="primary" size="medium" @click="dialogTableVisible = false">取消
          </el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import http, {service} from "@/http/request";
import Cookies from "js-cookie";
export default {
  data() {
    return {
      ruleFrom:{
        checkId:'',
        destroyId:'',
        destroyName:"",
        destroyNote:"",
        lyhCheckDetailsEntities:[],
      },
      tableDate:[],
      dialogTableVisible: false, //选择盘点药库的弹框
      currentPage:1, //初始页
      pagesize:10,   //    每页的数据
      destroyDetails:[],
      options:[],
    }
  },
  methods: {
    //大表格
    selectionLineChangeHandle2 (val) {
      this.destroyDetails = val;

      for(var i = 0; i< this.destroyDetails.length; i++) {
        console.log(this.destroyDetails[i].checkKnum)
        console.log(this.destroyDetails[i].drugId)
        console.log(this.destroyDetails[i].piCi)
      }
    },

    //新增销毁记录表
    addDestroy() {
      service.post("add-destroy",this.ruleFrom)
      for(var i = 0; i< this.destroyDetails.length; i++) {
        service.get("update-destroyState",{params:{
          checkState:3,
          checkId:this.destroyDetails[i].checkId
          }})
        service.get("update-destroy", {params:{
          checkKnum:this.destroyDetails[i].checkKnum2,
           drugId:this.destroyDetails[i].drugId,
           piCi:this.destroyDetails[i].piCi
          }})
        .then(v=>{
          this.clearDrugCheck();

        })
      }




      this.$message("销毁成功")
    },


    randomNumber() {
      const now = new Date()
      let month = now.getMonth() + 1
      let day = now.getDate()
      let hour = now.getHours()
      let minutes = now.getMinutes()
      let seconds = now.getSeconds()
      month = this.setTimeDateFmt(month)
      hour = this.setTimeDateFmt(hour)
      minutes = this.setTimeDateFmt(minutes)
      seconds = this.setTimeDateFmt(seconds)
      return now.getFullYear().toString() + month.toString() + day + hour + minutes + seconds + (Math.round(Math.random() * 23 + 100)).toString()
    },

    //清空盘点单
    clearDrugCheck() {
      this.ruleFrom.checkId="";
      this.ruleFrom.lyhCheckDetailsEntities=[];
      this.ruleFrom.destroyId="";
      this.ruleFrom.destroyName="";
      this.ruleFrom.destroyNote="";

    },
    //保留小数点后两位
    towNumber(val) {
      return val.toFixed(2)
    },
    updateById(checkState){

    },

    initDate(){
      service.get("find-findByXiaohui",{params:{
        checkId:this.ruleFrom.checkId
        }})
          .then((v) => {
            this.tableDate=v.data;
            this.ruleFrom.checkId=this.tableDate[0].checkId;
            this.ruleFrom.lyhCheckDetailsEntities=this.tableDate[0].lyhCheckDetailsEntities;
            this.ruleFrom.destroyId="XH"+this.randomNumber();
          })
    },

    findByPan(){
      service.get("find-checkId")
      .then(v=>{
        this.options=v.data;
      })
    },

    // 初始页currentPage、初始每页数据数pagesize和数据data
    handleSizeChange: function (size) {
      this.pagesize = size;
      console.log(this.pagesize)  //每页下拉显示数据
    },
    handleCurrentChange: function(currentPage){
      this.currentPage = currentPage;
      console.log(this.currentPage)  //点击第几页
    },
    setTimeDateFmt(month) {
      return 0;
    }
  },
  created() {
    this.findByPan();
    this.ruleFrom.destroyName=Cookies.get('username');
  },
  //计算属性
  computed: {


  },
  watch: {


  },


}
</script>


